const request = require('../../utils/https.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    card_number: '',
    phone: '',
    visible6: false,
    actions6: [{
        name: '取消'
      },
      {
        name: '确定',
        color: '#ed3f14'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log(options.type)
    that.setData({
      type: options.type
    })
    request._get(getApp().Api_url + 'user/hasVerify', {
      userId: wx.getStorageSync("userId")
    }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          username: res.data.data.username,
          card_number: res.data.data.idCard,
          phone: res.data.data.phone,
          nameHas: res.data.data.has, //0 没有提交过，1提交过
          sameWx: res.data.data.sameWx //0没绑定成功，1绑定成功
        })
      }
    }, function () {})
  },
  /*
    获取身份证号
  */
  getCard(e) {
    const that = this;
    that.setData({
      card_number: e.detail
    })
  },
  //获取手机号
  getPhone(e) {
    const that = this;
    that.setData({
      phone: e.detail
    })
  },
  /*
    获取真实姓名
  */
  getUserName(e) {
    const that = this;
    that.setData({
      username: e.detail
    })
  },
  postSubmit() {
    const that = this;
    let regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    var card_number = that.data.card_number;
    var username = that.data.username;
    var phone = that.data.phone;
    if (!username) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return false;
    }
    if (!regIdNo.test(card_number)) {
      wx.showToast({
        title: '身份证号填写有误',
        icon: 'none'
      })
      return false;
    }
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none'
      })
      return false;
    }


    that.setData({
      visible6: true
    })



  },
  handleClick6({
    detail
  }) {
    const that = this;
    if (detail.index === 0) {
      that.setData({
        visible6: false
      });
      console.log('取消')
    } else {
      console.log('确认')
      that.postForm();
      that.setData({
        visible6: false
      });

    }
  },
  postForm() {
    const that = this;
    var card_number = that.data.card_number;
    var username = that.data.username;
    var phone = that.data.phone;
    request._post(getApp().Api_url + 'user/doVerify', {
      userId: wx.getStorageSync("userId"),
      username: username,
      phone: phone,
      idCard: card_number
    }, function (res) {
      if (res.data.errCode == 200) {
        console.log('验证提交信息==>', res.data.data)
        wx.showToast({
          title: '保存成功',
          icon: 'none'
        })
        wx.navigateBack({
          delta: 1,
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }, function () {})
  },
})